﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using System.Data.SqlClient;
using Model;

namespace Dao
{
    public class CategoryDao
    {
        DBConnection dbConnect;
        public CategoryDao()
        {
            dbConnect = new DBConnection();
        }

        public DataTable getAllCategory()
        {
            string sql = "select * from category";
            return dbConnect.executeSelectQuery(sql);
        }


        public DataTable getCategoryById(string id)
        {
            string sql = "select * from category where CateId ="+id+"";
            return dbConnect.executeSelectQuery(sql);
        }

        public bool insertCategory(Category cate)
        {
            string query = string.Format(@"insert into Category(CateName , CateStatus) values (@cateName , @cateStatus)");
            SqlParameter[] sqlParameters = new SqlParameter[2];

            sqlParameters[0] = new SqlParameter("@cateName", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(cate.CateName);
            sqlParameters[1] = new SqlParameter("@cateStatus", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(cate.CateStatus);

            return dbConnect.executeInsertQuery(query, sqlParameters);
        }

        public bool updateCategory(Category cate)
        {
            string query = string.Format("Update category set cateName  =@cateName , cateStatus =@cateStatus where CateId =@id");
            SqlParameter[] sqlParameters = new SqlParameter[3];

            sqlParameters[0] = new SqlParameter("@cateName", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(cate.CateName);

            sqlParameters[1] = new SqlParameter("@cateStatus", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(cate.CateStatus);

            sqlParameters[2] = new SqlParameter("@id", SqlDbType.Int);
            sqlParameters[2].Value = Convert.ToString(cate.CategoryId);

            return dbConnect.executeUpdateQuery(query, sqlParameters);
        }

        public bool deleteCategory(string id)
        {
            string query = string.Format("delete category  where CateId =@id");
            SqlParameter[] sqlParameters = new SqlParameter[1];

            sqlParameters[0] = new SqlParameter("@id", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(id);

            return dbConnect.executeDeleteQuery(query, sqlParameters);
        }
    }
}
